package com.javaex.book.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javaex.book.pojo.Cart;
import com.javaex.book.pojo.OrderItem;
import com.javaex.book.pojo.User;
import com.javaex.book.services.OrderService;
import com.javaex.book.utils.Constants;
import com.javaex.book.utils.Pagination;
import com.javaex.book.utils.ReturnResult;

@Controller
@RequestMapping("/order")
@ResponseBody
public class OrderController {
	
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
	
	private final OrderService orderService;
	
	@Autowired
	public OrderController(OrderService orderService) {
		this.orderService = orderService;
	}
	
	/**
	 * 获取我的订单
	 * @param pageNo 页码
	 * @param pageSize 页容量
	 * @param session
	 * @return 分页信息和该页数据
	 */
	@GetMapping()
	public Pagination getMyOrder(
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, 
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, 
			HttpSession session) {
		logger.info("我的订单：pageNo={}, pageSize={}", pageNo, pageSize);
		User loginUser = (User) session.getAttribute(Constants.LOGIN_USER_KEY);
		if (loginUser == null) return new Pagination(pageNo, pageSize, 0);
		
		return orderService.getOrderList(loginUser.getId(), pageNo, pageSize);
	}
	
	/**
	 * 获取所有订单
	 * @param pageNo 页码
	 * @param pageSize 页容量
	 * @return 包含订单集合和分页信息的Pagination对象
	 */
	@GetMapping("/list")
	public Pagination getOrderList(
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, 
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		logger.info("全部订单：pageNo={}, pageSize={}", pageNo, pageSize);
		return orderService.getOrderList(null, pageNo, pageSize);
	}
	
	/**
	 * 获取订单详情
	 * @param orderId 订单id
	 * @return 订单项集合
	 */
	@GetMapping("/{orderId}")
	public List<OrderItem> getOrderDetail(@PathVariable("orderId") String orderId) {
		logger.info("订单详情：orderId={}", orderId);
		return orderService.getOrderDetail(orderId);
	}

	/**
	 * 购物车结算，生成订单
	 * @param session
	 * @return 订单结算结果
	 */
	@PostMapping()
	public ReturnResult createOrder(HttpSession session) {
		// 防止表单重复提交
		if (session.getAttribute(Constants.FORM_SUBMIT_NOT_REPEAT_KEY) == null) {
			return ReturnResult.fail("购物车已结算！");
		}
		session.removeAttribute(Constants.FORM_SUBMIT_NOT_REPEAT_KEY);
		
		// 验证用户是否登录
		User user = (User) session.getAttribute(Constants.LOGIN_USER_KEY);
		if (user == null) return ReturnResult.fail("用户未登录！");
		
		// 订单结算
		Cart cart = (Cart) session.getAttribute(Constants.CART_IN_SESSION_KEY);
		if (cart == null) return ReturnResult.fail("购物车为空！");
		
		ReturnResult result = orderService.createOrder(cart, user.getId());
		logger.info("购物车结算：{}", result.getData());
		if (result.isSuccess()) {
			// 清空购物车
			cart.clear();
		}
		return result;
	}
	
	/**
	 * 签收订单
	 */
	@PutMapping("/receiver/{orderId}")
	public ReturnResult receiverOrder(@PathVariable("orderId") String orderId) {
		logger.info("签收订单：id={}", orderId);
		return orderService.receiverOrder(orderId);
	}
	
	/**
	 * 订单发货
	 */
	@PutMapping("/send/{orderId}")
	public ReturnResult sendOrder(@PathVariable("orderId") String orderId) {
		logger.info("订单发货：id={}", orderId);
		return orderService.sendOrder(orderId);
	}
	
	/**
	 * 删除订单
	 * @param orderId 订单id
	 * @return 删除结果
	 */
	@DeleteMapping("/{orderId}")
	public ReturnResult deleteOrder(@PathVariable("orderId") String orderId) {
		logger.info("删除订单：id={}", orderId);
		return orderService.deleteOrder(orderId);
	}
}
